package com.redis.paihangbang.mapper;

import com.redis.paihangbang.entity.StudentScore;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;

import java.util.List;

@Mapper
public interface StudentScoreMapper {

    @Insert("INSERT INTO student_scores(student_id, student_name, score) VALUES(#{studentId}, #{studentName}, #{score})")
    void insert(StudentScore studentScore);

    @Update("UPDATE student_scores SET student_name = #{studentName}, score = #{score} WHERE student_id = #{studentId}")
    void update(StudentScore studentScore);

    @Select("SELECT * FROM student_scores WHERE student_id = #{studentId}")
    StudentScore findByStudentId(String studentId);

    @Select("<script>" +
            "SELECT * FROM student_scores WHERE student_id IN " +
            "<foreach collection='list' item='id' open='(' close=')' separator=','>" +
            "#{id}" +
            "</foreach>" +
            "</script>")
    List<StudentScore> findByStudentIds(List<String> studentIds);

    @Select("SELECT * FROM student_scores ORDER BY score DESC, update_time DESC LIMIT #{topN}")
    List<StudentScore> findTopN(@Param("topN") int topN);

    @Delete("DELETE FROM student_scores WHERE student_id = #{studentId}")
    int deleteByStudentId(String studentId);

    @Select("SELECT * FROM student_scores ORDER BY score DESC, update_time DESC")
    List<StudentScore> findAllOrderByScoreDesc();

    @Select("SELECT COUNT(1) FROM student_scores")
    long countAll();
}
